from flask import Blueprint,request,jsonify,session
from model import course,User,classInfo,college,statuscourse,uniquevalue
tea=Blueprint('teacher',__name__,url_prefix='/api/teacher')
@tea.route('/teacher',methods=['GET'])
def teacher():
    data = session.get('data')
    if data is None:
        return jsonify(code=400, message='请先登录')
    username = data['username']
    findteacher = User.query.filter_by(username=username).first()
    if findteacher is None:
        return jsonify(code=400, message='当前员工不存在')
    if findteacher.role != 'teacher':
        return jsonify(code=400, message='当前员工不是教师')
    findcourse = course.query.filter_by(teacher=username).all()
    data = []
    classiddata = []
    for i in findcourse:
      
        data.append({'id': i.id, 'coursename': i.coursename, 'timestamp': i.timestamp, 'classid': i.classid})
        if i.classid is not None:
            classiddata.append(i.classid)
    majorlist=[]
    for j in classInfo.query.filter(classInfo.id.in_(classiddata)).all():
        majorlist.append(j.major)
        for key in data:
            if key['classid']==j.id:
                key['classname'] = j.classname
                key['major'] = j.major
   
    majorlist=list(set(majorlist))
   
    for i in college.query.filter(college.major.in_(majorlist)).all():
        
        index=0
        for j in data:
            if j['major']==i.major:
                j['colegename']=i.collegename
                
                data[index]=j
            index+=1
   
    return jsonify(code=200, data=data)
@tea.route('/students')
def students():
    data = session.get('data')
    if data is None:
        return jsonify(code=400, message='请先登录')
    username = data['username']

    findcourse=course.query.filter_by(teacher=username).all()
    courseiddata=[]
    data=[]
    coursedict={}
    for i in findcourse:
        coursedict[i.id]={'classid':i.classid,'coursename':i.coursename,'courseid':i.id}
        courseiddata.append(i.id)
    usernamedata=[]
    for j in statuscourse.query.filter(statuscourse.courseid.in_(courseiddata)).all():
        usernamedata.append(j.username)
        userid=User.query.filter_by(username=j.username).first().id
        data.append({"username":j.username,"courseid":j.courseid,'id':j.id,'userid':userid})
    maxindex=0
   

    for i in data:
        index=0
        for j in coursedict.keys():
           if i['courseid'] == j:
               i['coursename']=coursedict[j]['coursename']
               i['classid']=coursedict[j]['classid']
               data[maxindex]=i
           index+=1
        maxindex+=1
   
    for index,i in enumerate(data):
            findnumberlist=uniquevalue.query.filter_by(userid=i['userid'],courseid=i['courseid']).all()
            number=0
            for j in findnumberlist:
                number+=j.number
            data[index]['number']=number
  
    return jsonify(code=200, data=data)
    

@tea.route('/getuniquenumber',methods=['GET'])
def getuniquelist():
    if request.method == 'GET':
        data = session.get('data')
        if data is None:
            return jsonify(code=400, message='请先登录')
        username = data['username']
        data=[]
        coureseidlist=[]
        findcourse=course.query.filter_by(teacher=username).all()
        for i in findcourse:
            data.append(dict(coursename=i.coursename,courseid=i.id))
            coureseidlist.append(i.id)
        for j in coureseidlist:
            number=0
            for i in uniquevalue.query.filter_by(courseid=j).all():
                number+=i.number
            for index,h in enumerate(data):
                if h['courseid']==j:
                    data[index]['number']=number
        return jsonify(code=200, data=data)
        